﻿using AutoMapper;
using MediatR;
using Management.API.Application.Controller.Management.Material;
using Management.API.DTOs.Management;
using Management.Infrastructure.Interfaces.BOMManagement;
using Management.Errorcode.ErrorCode;

namespace Management.API.Application.CommandHandler.Management.Material
{
    public class GetMaterialPropertyCommandHandler:IRequestHandler<GetMaterialPropertyCommand,ApiResult<IList<GetMaterialPropertyDTO>>>
    {
        private readonly IMaterialPropertyRepository iMaterialPropertyRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetMaterialPropertyCommandHandler> _logger;

        public GetMaterialPropertyCommandHandler(IMaterialPropertyRepository IMaterialPropertyRepository, IMapper mapper,
            ILogger<GetMaterialPropertyCommandHandler> logger)
        {
            iMaterialPropertyRepository = IMaterialPropertyRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetMaterialPropertyDTO>>> Handle(GetMaterialPropertyCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iMaterialPropertyRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetMaterialPropertyDTO>>(useList);
                return ApiResult<IList<GetMaterialPropertyDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetMaterialPropertyCommandHandler");
                throw;
            }

        }




    }
}
